package com.macro.mall.tiny.controller;

import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;


@RequestMapping("/minio")
@RestController
@Api(tags = "MinIO文件接口")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation(value = "使用minio文件上传")
    @PostMapping("/uploadFile")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "MultipartFile", name = "file", value = "上传的文件", required = true),
            @ApiImplicitParam(dataType = "String", name = "bucketName", value = "对象存储桶名称", required = false)
    })
    public CommonResult<String> uploadFile(@RequestParam(value = "bucketName",required = false) String bucketName,
                             @RequestParam(value = "file", required = true) MultipartFile file) {
        String filePath = fileService.uploadFile(file,bucketName);
        return CommonResult.success(filePath);
    }

    @ApiOperation("使用MinIO下载文件")
    @GetMapping("/downloadFile")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "filePath",value = "下载的文件",required = true),
            @ApiImplicitParam(dataType = "String",name = "bucketName",value = "对象存储桶名称",required = false)
    })
    public void downloadFile(@RequestParam("filePath") String filePath,@RequestParam(value = "bucketName",required = false) String bucketName,
                               HttpServletResponse response){
        fileService.download(filePath, bucketName,  response);
    }

    @ApiOperation("删除MinIO下载文件")
    @PostMapping("/deleteFile")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String",name = "filePath",value = "待删除的文件",required = true),
            @ApiImplicitParam(dataType = "String",name = "bucketName",value = "对象存储桶名称",required = true)
    })
    public void deleteFile(@RequestParam("filePath") String filePath, @RequestParam(value = "bucketName",required = false) String bucketName){
        fileService.delete(filePath, bucketName);
    }
}